package com.example.testproject.view_model

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.testproject.R
import com.example.testproject.adapter.TextAdapter
import com.example.testproject.adapter.TopicAdapter
import com.example.testproject.adapter.TopicSubmitAdapter
import com.example.testproject.bean.TopicSecondBean
import com.example.testproject.utils.SwitchDialogFragment

/**
 *
 * @ClassName: TopicSubmitViewModel
 * @Author: android 超级兵
 * @CreateDate: 10/29/21$ 9:19 AM$
 * TODO
 */
class TopicSubmitViewModel : ViewModel() {

    fun  showDialog(
        context: Context,
        supportFragmentManager: FragmentManager,
        topicSecondBean: TopicSecondBean,
        topicSubmitAdapter: TopicSubmitAdapter,
        type: Int,){

        val view = LayoutInflater.from(context)
            .inflate(R.layout.topic_item_second, null)
        view.layoutParams = ConstraintLayout.LayoutParams(-1, -2)

        val dialogFragment = SwitchDialogFragment(
            view,
            topicSubmitAdapter.typeJudge(topicSecondBean.type),
            context
        )

        dialogFragment.setBottomShow(true)
//
        val tvType = view.findViewById<TextView>(R.id.tvType)
        val topicRv = view.findViewById<RecyclerView>(R.id.topicRv)
        val submitTv = view.findViewById<TextView>(R.id.submitTv)
        // 标题
        tvType.text = topicSecondBean.title

        submitTv.visibility = View.VISIBLE

        // 其他选项
        var tempStr = ""
        // 如果是填空题
        var tempFill = ""
        topicSecondBean.child.forEachIndexed { position, data ->
            if (data.isSelect) {
                tempStr += "${(position + 65).toChar()}\t"
            }
            // 如果是填空题 记录输入内容
            if (type == TopicAdapter.FILL && data.isSelect) {
                tempFill += "${data.ContentTitle}\t"
            }
        }

        // 先要区分 是否是填空
        submitTv.text = String.format(
            "您提交的答案为:%s",
            if (type == TopicAdapter.FILL) tempFill else tempStr
        )

        // recyclerView 操作
        topicRv.layoutManager = LinearLayoutManager(context)

        topicRv.adapter =
            TextAdapter(topicSecondBean.child.map { it.ContentTitle }.toList())

        dialogFragment.show(supportFragmentManager, dialogFragment.tag)

    }
}